package com.gzdh.device.shop.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gzdh.device.comm.vo.Result;
import com.gzdh.device.shop.entity.ShopInfo;
import com.gzdh.device.shop.entity.ShopMachine;
import com.gzdh.device.shop.service.ShopInfoService;
import com.gzdh.device.shop.service.ShopMachineService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("api/shopMachine/v1")
public class ShopMachineController {


    @Autowired
    ShopMachineService shopMachineService;

    @Autowired
    ShopInfoService shopInfoService;

    @GetMapping("getShopMachine")
    public Result<Object> getShopMachine(@RequestParam("shopId") String shopId) {
        Result<Object> result = new Result<>();

        ShopMachine shopMachine = shopMachineService.getOne(new QueryWrapper<ShopMachine>().lambda().eq(ShopMachine::getShopId, shopId));
        result.setResult(shopMachine);

        return result;
    }

    @PostMapping("updateShopMachine")
    public Result<Object> updateShopMachine(@RequestBody JSONObject jsonObject) {
        Result<Object> result = new Result<>();
        String id = jsonObject.getString("id");
        String businessUrl = jsonObject.getString("businessUrl");
        String logicGroupId = jsonObject.getString("logicGroupId");
        String abcpAppid = jsonObject.getString("abcpAppid");
        String tenantId = jsonObject.getString("tenantId");
        boolean isTenant = jsonObject.getBoolean("isTenant");
        ShopMachine shopMachine = shopMachineService.getById(id);
        if (shopMachine == null) {
            return Result.error("更新失败");
        }
        shopMachine.setBusinessUrl(businessUrl);
        shopMachine.setLogicGroupId(logicGroupId);
        shopMachine.setAbcpAppid(abcpAppid);
        shopMachine.setTenantId(tenantId);
        shopMachine.setIsTenant(isTenant);
        shopInfoService.lambdaUpdate()
                .set(ShopInfo::getIsTenant, isTenant)
                .set(StringUtils.isNotEmpty(tenantId),ShopInfo::getTenantId, tenantId)
                .eq(ShopInfo::getId, shopMachine.getShopId())
                .update();
        shopMachineService.updateById(shopMachine);
        return result;
    }
}
